﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Game
{
    class UI
    {
        SlimeGame _myGame;
        Map _map;
        Player[] _players;
        public UI(SlimeGame game,ref Map map,ref Player[] players) {
            _myGame = game;
            _map = map;
            _players = players;

        }
        public SlimeGame GameData{
            get { return _myGame; }
        }
        public Map Map
        {
            get { return _map;}
        }

        public Player GetPlayer(int index)
        {
            return _players[index];
        }

        public void Display()
        {
            Console.Clear();
            _map.Update(_players[0], _players[1]);
            _map.Display();
            _players[0].Display('a');
            _players[1].Display('A');
        }

        public void Move(Slime S, Position Goal,bool show = true)
        {
            Move(S.ID, Goal,S.Side);
            if (show) Display();
        }
        public void Move(char ID, Position Goal)
        {
            char side = 'a';
            if (char.IsUpper(ID)) { side = 'A';}

            Move((int)(char.ToLower(ID)-'a'), Goal,side);
        }
        public void Move(int ID, Position Goal, char side)
        {
             if(char.IsLower(side)){
                 if (_players[0].IsExistence(ID)) {
                     _myGame.Move(ID, Goal, side);     
                 }
             }else{
                 if (_players[1].IsExistence(ID)) {
                     _myGame.Move(ID, Goal, side);     
                 }
             }
            
        }
        public void Kill(int ID, char side) {
            
            if (char.IsLower(side)) {
                Slime temps = new Mother('c', 100, _players[0].Slimes[ID].X, _players[0].Slimes[ID].Y + 1);
                for(int i =0;i<_players[0].Slimes.Count;i++){
                    if(_players[0].Slimes[i].ID==ID) {temps = _players[0].Slimes[i];break;}
                }
                       temps.HP = 0;
                       _myGame.NoshowAtk(temps, temps);
                
            } else {
                Slime temps = new Mother('c', 100, _players[1].Slimes[ID].X, _players[1].Slimes[ID].Y + 1);
                for (int i = 0 ; i < _players[1].Slimes.Count ; i++) {
                    if (_players[1].Slimes[i].ID == ID) { temps = _players[1].Slimes[i]; break; }
                }
                temps.HP = 0;
                _myGame.NoshowAtk(temps, temps);
                
            }
        }

        public void Atk(char myc, char youc,bool show = true)
        {
            if (_players[1].IsExistence(myc) || _players[0].IsExistence(myc) && _players[1].IsExistence(youc) || _players[0].IsExistence(youc))
            {
                Slime my;
                Slime you;
                if (char.IsUpper(myc))
                {
                    my = _players[1].getID(myc);
                }
                else
                {
                    my = _players[0].getID(myc);
                }
                if (char.IsUpper(youc))
                {
                    you = _players[1].getID(youc);
                }
                else
                {
                    you = _players[0].getID(youc);
                }
                Atk(my, you,show);
            }
        }

        public void Atk(Slime my, Slime you, bool show = true)
        {
            if (show)
            {
                _myGame.Atk(my, you);
            }
            else
            {
                _myGame.NoshowAtk(my, you);
            } 
        }
        public void State(char ID)
        {
            if (_players[1].IsExistence(ID) || _players[0].IsExistence(ID))
            {
                Slime my;
                if (char.IsUpper(ID))
                {
                    my = _players[1].getID(ID);
                }
                else
                {
                    my = _players[0].getID(ID);
                }
                _myGame.State(my);
            }
           
        }


        public void Born(char cat, Position P)
        {
            _myGame.Born(cat, P);
        }
        public void Born(char cat, int x, int y)
        {
            Position P = new Position(x, y);
            Born(cat, P);
        }
        public void Born(SlimesCategory SC, Position P)
        {
            char ID = 'K';
            switch (SC)
            {
                case SlimesCategory.ARCHER:
                    ID = 'A';
                    break;
                case SlimesCategory.KNIGHT:
                    ID = 'K';
                    break;
                case SlimesCategory.PAWN:
                    ID = 'A';
                    break;
            }
            Born(ID, P);
        }
        public void Rest(char ID)
        {
            _myGame.Rest(ID);  
           
        }

        public void Rest()
        {
            _myGame.Rest();           
        }            

        public int Over()
        {
            return _myGame.Over();
        }

        public int TurnToWho
        {
            get { return _myGame.TurnToWho; }
        }

    }
}
